/**
******************************************************************************
* @File:
* @Author:	Ares
* @Revision:
* @Date:
* @Brief:	energy sample serial port
******************************************************************************
* @attention
*
******************************************************************************
*/
#include "dev.h"
#include "sys.h"
#include "conf.h"

u8	U3_RCACHE[U3_RMAX]={0};
u16	U3_RLEN=0;



void usart3_sendbyte(u8 ch)
{
	USART_SendData(USART3,ch);
	while(USART_GetFlagStatus(USART3, USART_FLAG_TXE)==RESET);
}

void usart3_sendstring(u8 *str,u16 len)
{
	for(u16 i=0;i<len;i++)
	{
		usart3_sendbyte(str[i]);
	}
}


#if (ENERGY_SAMPLE_MODE==MODE_RN8209D)
void USART3_IRQHandler(void)
{
	u8 buf[20]={0};
	if(USART_GetITStatus(USART3,USART_IT_RXNE)!=RESET)
	{
		U3_RCACHE[U3_RLEN]=USART_ReceiveData(USART3);
		U3_RLEN++;
	}
}
#elif (ENERGY_SAMPLE_MODE==MODE_WASON)
void USART3_IRQHandler(void)	
{
	u16 i;
 	if(USART_GetITStatus(USART3, USART_IT_IDLE) != RESET)
	{
		DMA_Cmd(DMA1_Channel3, DISABLE);
		U3_RLEN=U3_RMAX-DMA_GetCurrDataCounter(DMA1_Channel3);
		//*************************************//
		//DMA_ClearFlag(DMA1_FLAG_GL3 | DMA1_FLAG_TC3 | DMA1_FLAG_TE3 | DMA1_FLAG_HT3);//???
		DMA1_Channel3->CNDTR = U3_RMAX;
		DMA_Cmd(DMA1_Channel3, ENABLE);
		
		//em_process();
		memset(U3_RCACHE,0,U3_RLEN); //reset rcv bytes after cache dump
		
		i = USART3->SR;
		i = USART3->DR;
	}
	//if(USART_GetITStatus(USART3, USART_IT_PE | USART_IT_FE | USART_IT_NE) != RESET)//??
	{
		//printf("dma err 33!!!!!!!!!!!!!!!!!!\r\n");
		//USART_ClearITPendingBit(USART3, USART_IT_PE | USART_IT_FE | USART_IT_NE);
	}
	//USART_ClearITPendingBit(USART3, USART_IT_IDLE);
}
#endif





void usart3_init(void)
{
	GPIO_InitTypeDef	GPIO_InitStructure;
	USART_InitTypeDef	USART_InitStructure;
	NVIC_InitTypeDef	NVIC_InitStructure;
	
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3,ENABLE);
	
	USART_DeInit(USART3);
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; 	//USART3_TX PB.10
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
	GPIO_Init(GPIOB, &GPIO_InitStructure);
	
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;		//USART3_RX PB.11
#if (ENERGY_SAMPLE_MODE==MODE_RN8209D)
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
#elif (ENERGY_SAMPLE_MODE==MODE_WASON)
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
#endif
	GPIO_Init(GPIOB, &GPIO_InitStructure);
	
	NVIC_InitStructure.NVIC_IRQChannel = USART3_IRQn;
	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 3;
	NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2;
	NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
	NVIC_Init(&NVIC_InitStructure);
	
	USART_InitStructure.USART_BaudRate = 4800;
	USART_InitStructure.USART_WordLength = USART_WordLength_9b;
	USART_InitStructure.USART_StopBits = USART_StopBits_1;
	USART_InitStructure.USART_Parity = USART_Parity_Even;
	USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
	USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;

	USART_Init(USART3, &USART_InitStructure);
#if (ENERGY_SAMPLE_MODE==MODE_RN8209D)
	USART_ITConfig(USART3, USART_IT_RXNE, ENABLE);
#elif (ENERGY_SAMPLE_MODE==MODE_WASON)
	USART_ITConfig(USART3, USART_IT_IDLE, ENABLE);
#endif
	USART_Cmd(USART3, ENABLE);

}

void usart3_dma_init(void)
{
	DMA_InitTypeDef DMA_InitStructure;
	RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);

	USART_DMACmd(USART3, USART_DMAReq_Rx, ENABLE);
	
	DMA_DeInit(DMA1_Channel3);
	DMA_InitStructure.DMA_PeripheralBaseAddr=(u32)&USART3->DR;
	DMA_InitStructure.DMA_MemoryBaseAddr=(u32)U3_RCACHE;
	DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC;
	DMA_InitStructure.DMA_BufferSize=U3_RMAX;
	DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
	DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
	DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;
	DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte;

	DMA_InitStructure.DMA_Mode = DMA_Mode_Normal;  
	DMA_InitStructure.DMA_Priority = DMA_Priority_Medium;
	DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
	DMA_Init(DMA1_Channel3,&DMA_InitStructure);  

	DMA_Cmd(DMA1_Channel3,ENABLE);
}
